package test.serverframe.armc.server.manager.dao.mapper;

import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import test.serverframe.armc.server.manager.controller.vo.TaskHistoryVo;
import test.serverframe.armc.server.manager.domain.InvokeTaskRecord;
import test.serverframe.armc.server.manager.domain.params.HistoryRecordParam;
import test.serverframe.armc.server.manager.domain.params.TaskImplementParams;

import java.util.List;

public interface InvokeTaskRecordMapper {
    @Delete({
            "delete from tb_ssgp_invoke_task_record",
            "where invoke_task_record_id = #{invokeTaskRecordId,jdbcType=VARCHAR}"
    })
    int deleteByPrimaryKey(String invokeTaskRecordId);

    @Insert({
    	"insert into tb_ssgp_invoke_task_record (invoke_task_record_id, invoke_record_id, ",
        "status, interface_total, start_time, end_time,",
        "interface_success_total, task_group_id, version, node_arr)",
        "values (#{invokeTaskRecordId,jdbcType=VARCHAR}, #{invokeRecordId,jdbcType=VARCHAR}, ",
        "#{status,jdbcType=BIT}, #{interfaceTotal,jdbcType=VARCHAR}, ",
        "#{startTime,jdbcType=TIMESTAMP},#{endTime,jdbcType=TIMESTAMP},#{interfaceSuccessTotal,jdbcType=VARCHAR}, ",
        "#{taskGroupId,jdbcType=VARCHAR},#{version,jdbcType=TIMESTAMP},#{nodeArr,jdbcType=VARCHAR})"
    })
    int insert(InvokeTaskRecord record);

    @InsertProvider(type = InvokeTaskRecordSqlProvider.class, method = "insertSelective")
    int insertSelective(InvokeTaskRecord record);

    @Select({
            "select",
            "invoke_task_record_id, invoke_record_id, status, interface_total, interface_success_total, ",
            "task_group_id, version, node_arr",
            "from tb_ssgp_invoke_task_record",
            "where invoke_task_record_id = #{invokeTaskRecordId,jdbcType=VARCHAR}"
    })
    @Results({
            @Result(column = "invoke_task_record_id", property = "invokeTaskRecordId", jdbcType = JdbcType.VARCHAR, id = true),
            @Result(column = "invoke_record_id", property = "invokeRecordId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "interface_total", property = "interfaceTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "interface_success_total", property = "interfaceSuccessTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "task_group_id", property = "taskGroupId", jdbcType = JdbcType.VARCHAR),
            @Result(column = "version", property = "version", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "node_arr", property="nodeArr", jdbcType=JdbcType.VARCHAR),
    })
    InvokeTaskRecord selectByPrimaryKey(String invokeTaskRecordId);

    @UpdateProvider(type = InvokeTaskRecordSqlProvider.class, method = "updateByPrimaryKeySelective")
    int updateByPrimaryKeySelective(InvokeTaskRecord record);

    @Update({
            "update tb_ssgp_invoke_task_record",
            "set invoke_record_id = #{invokeRecordId,jdbcType=VARCHAR},",
            "status = #{status,jdbcType=BIT},",
            "interface_total = #{interfaceTotal,jdbcType=VARCHAR},",
            "interface_success_total = #{interfaceSuccessTotal,jdbcType=VARCHAR},",
            "task_group_id = #{taskGroupId,jdbcType=VARCHAR},",
            "version = #{version,jdbcType=TIMESTAMP},",
            "node_arr = #{nodeArr,jdbcType=VARCHAR},",
            "where invoke_task_record_id = #{invokeTaskRecordId,jdbcType=VARCHAR}"
    })
    int updateByPrimaryKey(InvokeTaskRecord record);

    @Select({
            " SELECT" +
                    " task_name," +
                    " record.STATUS," +
                    " record.interface_total as interfaceTotal,  " +
                    " record.interface_success_total as interfaceSuccessTotal,  " +
                    " record.start_time as startTime,  " +
                    " record.end_time as endTime" +
                    " FROM" +
                    " tb_ssgp_invoke_task_record record," +
                    " tb_ssgp_task task ",
            " WHERE" +
                    " record.task_group_id = task.task_group_id " +
                    " AND record.version = task.version "+
                    " AND record.invoke_record_id = #{taskInvokeId,jdbcType=VARCHAR}"+
                    " ORDER BY record.start_time DESC "
    })
    @Results({
            @Result(column = "task_name", property = "taskName", jdbcType = JdbcType.VARCHAR),
            @Result(column = "record.status", property = "status", jdbcType = JdbcType.BIT),
            @Result(column = "record.interface_total", property = "interfaceTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "record.interface_success_total", property = "interfaceSuccessTotal", jdbcType = JdbcType.VARCHAR),
            @Result(column = "record.start_time", property = "startTime", jdbcType = JdbcType.TIMESTAMP),
            @Result(column = "record.end_time", property = "endTime", jdbcType = JdbcType.TIMESTAMP),

    })
    List<TaskImplementParams> selectTaskImplement( @Param(value="taskInvokeId")String taskInvokeId);

    @Select({
    	
    	"SELECT node_arr, invoke_task_record_id",
    	"FROM tb_ssgp_invoke_task_record",
    	"WHERE invoke_record_id = #{invokeRecordId,jdbcType=VARCHAR} AND version = (",
	    	"SELECT version",
	    	"FROM tb_ssgp_task",
	    	"WHERE task_id = #{taskId,jdbcType=VARCHAR}",
    	") AND task_group_id = (",
	    	"SELECT task_group_id",
	    	"FROM tb_ssgp_task",
	    	"WHERE task_id = #{taskId,jdbcType=VARCHAR}",
		")"
    	/*"SELECT tk.task_name,tk.task_id",
    	"FROM tb_ssgp_task AS tk, tb_ssgp_task_enter_invoke AS tei",
    	"WHERE tei.task_id = tk.task_id AND tei.task_invoke_id = #{taskInvokeId,jdbcType=VARCHAR} AND tei.task_id = (",
	    	"SELECT task_id",
	    	"FROM tb_ssgp_task",
	    	"WHERE version = #{version,jdbcType=TIMESTAMP} and task_group_id = #{taskGroupId,jdbcType=VARCHAR}",
    	")"*/
    })
    @Results({
    	@Result(column = "invoke_task_record_id", property = "taskRecordId", jdbcType = JdbcType.VARCHAR),
    	@Result(column = "node_arr", property="nodeArr", jdbcType=JdbcType.VARCHAR),
    	@Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
    })
    List<TaskHistoryVo> getTaskRecord(HistoryRecordParam record);

	/*@Select({
		"SELECT version,task_group_id,invoke_task_record_id,status",
		"FROM tb_ssgp_invoke_task_record",
		"WHERE invoke_record_id = #{invokeRecordId,jdbcType=VARCHAR}"
	})
	 @Results({
		 @Result(column = "invoke_task_record_id", property = "taskRecordId", jdbcType = JdbcType.VARCHAR),
         @Result(column = "task_group_id", property = "taskGroupId", jdbcType = JdbcType.VARCHAR),
         @Result(column = "status", property = "status", jdbcType = JdbcType.BIT),
         @Result(column = "version", property = "version", jdbcType = JdbcType.TIMESTAMP)
	})
	List<HistoryRecord> findByInvokeRecordId(String invokeRecordId);*/
}